﻿using System;
using Autodesk.Revit.DB;
using YArchitech.Revit;

namespace YArchitech.HVAC.AuxiliaryCurve
{
	public class AngleHandle : AuxiliaryHandldSelPoints
	{
		public AngleHandle(ref XYZ minArgu, ref XYZ maxArgu, ref XYZ anglePtArgu, double xSideArgu, double ySideArgu, bool bIsNumberSet = false)
		{
			this.Handle(ref minArgu, ref maxArgu, ref anglePtArgu, xSideArgu, ySideArgu, bIsNumberSet);
		}

		private bool Handle(ref XYZ min, ref XYZ max, ref XYZ anglePt, double xSide, double ySide, bool bIsNumberSet = false)
		{
			XYZ xyz = (max - min).Normalize();
			YJKLine hyline = new YJKLine(min, max);
			hyline.MakeUnBound();
			XYZ project = hyline.GetProject(anglePt);
			XYZ xyz2 = (anglePt - project).Normalize();
			if (bIsNumberSet)
			{
				min = min + ySide * xyz2 + xSide * xyz;
				max = max + ySide * xyz2 - xSide * xyz;
				hyline = new YJKLine(max, max + xyz2);
				hyline.MakeUnBound();
				if (!hyline.IsPointOnLine(anglePt))
				{
					anglePt = hyline.GetProject(anglePt);
				}
				anglePt -= ySide * xyz2;
			}
			else
			{
				min = min + ySide * xyz2 + xSide * xyz;
				max += ySide * xyz2;
				hyline = new YJKLine(max, max + xyz2);
				hyline.MakeUnBound();
				if (!hyline.IsPointOnLine(anglePt))
				{
					anglePt = hyline.GetProject(anglePt);
				}
			}
			return true;
		}
	}
}
